package com.tt.vtg.controller;


import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tt.vtg.dao.entity.User;

@Controller
public class LoginController {

    @ApiOperation(value = "用户登录", notes = "")
	@GetMapping(value="/login")
    public String login(){
        return "login/login";
    }

    @PostMapping(value="/login")
    public String login(HttpServletRequest request, User user,boolean rememberMe, Model model){
        if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPassword())) {
            request.setAttribute("msg", "用户名或密码不能为空！");
            return "login/login";
        }
        System.out.println(rememberMe);
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(),user.getPassword(),rememberMe);
        try {
            subject.login(token);
            return "redirect:/index";
        }catch (LockedAccountException lae) {
            token.clear();
            request.setAttribute("msg", "用户已经被锁定不能登录，请与管理员联系！");
            return "login/login";
        } catch (AuthenticationException e) {
            token.clear();
            request.setAttribute("msg", "用户或密码不正确！");
            return "login/login";
        }
    }
    @GetMapping(value="/index")
    public String index(){
        return "index/index";
    }
    @GetMapping(value="/403")
    public String error403(){
    	return "403";
    }
}
